package edu.hnu.cloudflightserver.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;

public class HttpUtil {

	/**
	 * 默认的Http连接超时时间
	 */
	public static  int timeout = 2;
	
	public static String encode="utf-8";
	
	
	
	
	public static String getStringByGet(String path) throws IOException{
		return getStringByGet(path, null);
	}
	
	/**
	 * 
	 * @param path
	 * @return
	 * @throws IOException
	 */
	public static String getStringByGet(String path,Map<String, String> params) throws IOException {

		if(params!=null&&params.size()>0){
			StringBuilder url = new StringBuilder(path);
			url.append("?");
			for(Map.Entry<String, String> entry : params.entrySet()){
				url.append(entry.getKey()).append("=");
				url.append(URLEncoder.encode(entry.getValue(), encode));
				url.append("&");
			}
			url.deleteCharAt(url.length() - 1);
			path=url.toString();
		}
		
		URL u = new URL(path);
		HttpURLConnection connection = (HttpURLConnection) u.openConnection();
		connection.setConnectTimeout(timeout * 1000);
		connection.setRequestMethod("GET");
		if (connection.getResponseCode() == 200) {
			BufferedReader in = new BufferedReader(new InputStreamReader(
					u.openStream(), encode));
			StringBuilder sb = new StringBuilder();
			String line;
			while ((line = in.readLine()) != null) {
				sb.append(line);
			}
			return sb.toString();
		}
		return null;
	}

	public static String getStringByPost(String path, Map<String,String> params) throws IOException{
		StringBuilder data = new StringBuilder();
		if(params!=null && !params.isEmpty()){
			for(Map.Entry<String, String> entry : params.entrySet()){
				data.append(entry.getKey()).append("=");
				data.append(URLEncoder.encode(entry.getValue(), encode));
				data.append("&");
			}
			data.deleteCharAt(data.length() - 1);
		}
		byte[] entity = data.toString().getBytes();//生成实体数据
		URL u = new URL(path);
		HttpURLConnection conn = (HttpURLConnection) u.openConnection();
		conn.setConnectTimeout(5000);
		conn.setRequestMethod("POST");
		conn.setDoOutput(true);//允许对外输出数据
		conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		conn.setRequestProperty("Content-Length", String.valueOf(entity.length));
		OutputStream outStream = conn.getOutputStream();
		outStream.write(entity);
		if(conn.getResponseCode() == 200){
			BufferedReader in = new BufferedReader(new InputStreamReader(
					u.openStream(), encode));
			StringBuilder sb = new StringBuilder();
			String line;
			while ((line = in.readLine()) != null) {
				sb.append(line);
			}
			return sb.toString();
		}
		return null;

	}

	
	

	/*public static boolean isNetworkAvailable(Context context) {
		ConnectivityManager connectivity = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity == null) {
			return false;
		} else {// 获取所有网络连接信息
			NetworkInfo[] info = connectivity.getAllNetworkInfo();
			if (info != null) {// 逐一查找状态为已连接的网络
				for (int i = 0; i < info.length; i++) {
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		}
		return false;
	}*/
}
